// 把udp 消息转成joy

#include "udp/udp_server.hpp"
#include <mutex>

#define Djson neb::CJsonObject

uint64_t recv_n = 0;
uint64_t recv_data = 0;

class MulticastTest : public ADEALL_UDP_SERVER
{
private:
    // udp——监听端口
    int port = 8887;

public:
    MulticastTest(/* args */);
    ~MulticastTest();
    void loop()
    {
    }

private:
    // udp接收的回调函数
    void CallBackFuntion(std::string ip, int port, std::vector<uint8_t> data, void *)
    {

        recv_n++;
        recv_data = (*((uint64_t *)&data[0]));
        printf("%s [%d]", ip.c_str(), port);

        printf("data %ld [", data.size());
        for (size_t i = 0; i < data.size(); i++)
        {
            printf("%X ", data[i]);
        }

        printf("]  data= %llu recv_n=%llu recv_data =%llu \n", (*((uint64_t *)&data[0])), recv_n, recv_data);
        // std::cout << (*((uint64_t *)&data[0])) << "  " << recv_n << "  " << recv_data << std::endl;
    }
};

MulticastTest mq;

void *PrintfInfo(void *prt)
{
    while (1)
    {
        // printf("接收计数:%llu   data=:%llu    差值:%llu\n", recv_n, recv_data, (recv_data - recv_n));
        std::cout << mq.get_running_N() << " qs=" << mq.get_msg_N() << " recv_n=" << recv_n << "  data=" << recv_data << "  error=" << (recv_data - recv_n) << std::endl;
        sleep_ms(500);
    }
};

MulticastTest::MulticastTest(/* args */)
{

    ADEALL_UDP_SERVER::Init(42102, this);
    ADEALL_UDP_SERVER::AddMulticast("224.0.2.2", "192.168.2.30");
    // fd = -1;
    // fd = ADEALL_UDP_SERVER::create_Ip_fd("0.0.0.0", -1);//给定一个发送接口
}

MulticastTest::~MulticastTest()
{
}

int main(int argc, char **argv)
{
    ThreadBase(PrintfInfo, nullptr);

    while (1)
    {
        mq.loop();
        sleep_ms(500);
    }
    return 0;
}
